package parse;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.security.PublicKey;
import java.util.HashMap;
import java.util.List;


public class XMLParse {
    private static HashMap<String,String> hashMap = new HashMap<String,String>();

    public XMLParse() {
        parse();
    }

    public static String Get(String url_pattern){
        String servlet_class = hashMap.get(url_pattern);
        return servlet_class;
    }

    public void parse(){
        try {

            //创建 SAXReader实例
            SAXReader saxReader = new SAXReader();
            //得到xml文档对象
            Document document = saxReader.read(new BufferedInputStream(new FileInputStream("TOMCAT1/webapp/WEB_INF/web.xml")));
            //得到根标签对象
            Element rootElement = document.getRootElement();
            //获得
            List<Element> elements1 = rootElement.elements("servlet");
            List<Element> elements2 = rootElement.elements("servlet-mapping");

            for (Element element1:elements1) {
                for (Element element2:elements2) {
                    if (element1.element("servlet-name").getText().equals(element2.element("servlet-name").getText())){
                        hashMap.put(element2.element("url-pattern").getText(),element1.element("servlet-class").getText());
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }





}
